<?php
/**
 * Handler File Class
 *
 * @author admin <admin@wolive.cc>
 * @email admin@wolive.cc
 * @date 2017/06/01
 */

namespace app\index\controller;

use think\Controller;
use app\extra\push\Pusher;
use app\index\model\User;
use app\Common;

/**
 *
 * 前台Pc端对话窗口.
 */
class Index extends Controller
{

    /**
     *
     * [home description]
     * @return [type] [description]
     */
    public function home()
    {

        $data = $this->request->request();

        $data['product'] = empty($data['product']) ? '' : $data['product'];

        // visiter_id 没传递，生成一个
        $data['visiter_id'] = ($data['visiter_id']);
        if ($data['visiter_id'] === '') {
            if (empty($_SESSION['Visiter']['visiter_id'])) {
                $data['visiter_id'] = str_replace(['+','/', '='] ,'' , base64_encode(pack('Nn', time(), rand(1, 65535))));
                $_SESSION['Visiter']['visiter_id'] = $data['visiter_id'];
            } else {
                $data['visiter_id'] = $_SESSION['Visiter']['visiter_id'];
            }
        }

        // 传递商品信息
        if ($data['product']) {
            $product = json_decode(htmlspecialchars_decode($data['product']), true);
            if (isset($product['pid']) && isset($product['url']) && isset($product['img']) && isset($product['title']) && isset($product['info']) && isset($product['price'])) {

                $arr2['timestamp'] = time();

                $service_id = 0;

                $str = '<a href="' . ($product['url']) . '" target="_blank" class="wolive_product_msg">';
                $str .= '<img src="' . ($product['img']) . '" >';
                $str .= '<div class="product_info"><p class="wolive_product_title">' . ($product['title']) . '</p>';
                $str .= '<p class="wolive_product_info">' . ($product['info']) . '</p>';
                $str .= '<p class="wolive_product_price">' . ($product['price']) . '</p>';
                $str .= '</div></a>';

                $mydata = ['service_id' => ($service_id), 'visiter_id' => $data['visiter_id'], 'content' => $str, 'timestamp' => time(), 'business_id' => ($data['business_id']), 'direction' => 'to_service'];

                User::table('wolive_chats')->insert($mydata);
            }
        }

        $service_name = isset($data['service_name']) ? $data['service_name'] : '';

        $str = "visiter_id=" . $data['visiter_id'] . "&visiter_name=" . $data['visiter_name'] . "&avatar=" . $data['avatar'] . "&business_id=" . $data['business_id'] . "&service_name=$service_name" . "&groupid=" . $data['groupid'];

        $common = new Common();

        $newstr = $common->encrypt($str, 'E', 'wolive');

        $a = urlencode($newstr);

        $this->redirect('/index/index?code=' . $a);

    }

    /**
     * 对话窗口页面.
     *
     * @return mixed
     */

//http://megolive.test/index/index
    public function index()
    {

        $sarr = parse_url(ahost);
        if ($sarr['scheme'] == 'https') {
            $state = true;
        } else {
            $state = false;
        }

        $app_key = app_key;
        $app_secret = app_secret;
        $app_id = app_id;
        $options = array(
            'encrypted' => $state
        );
        $host = ahost;
        $port = aport;

        $pusher = new Pusher(
            $app_key,
            $app_secret,
            $app_id,
            $options,
            $host,
            $port
        );

        $common = new Common();

        $is_mobile = $common->isMobile();

        $url = domain;

        if (isset($_SERVER['HTTP_REFERER'])) {
            $from_url = $_SERVER['HTTP_REFERER'];
        } else {
            $from_url = '';
        }


        $arr = $this->request->get();

        $data = $common->encrypt($arr['code'], 'D', 'wolive');

        if (!$data) {
            $this->redirect('index/index/errors');
        }

        parse_str($data, $arr2);

        if (!isset($arr2['visiter_id']) || !isset($arr2['visiter_name']) || !isset($arr2['groupid']) || !isset($arr2['business_id']) || !isset($arr2['avatar'])) {
            $this->redirect('index/index/errors');
        }

        // visiter_id 没传递，生成一个
        $arr2['visiter_id'] = ($arr2['visiter_id']);
        if ($arr2['visiter_id'] === '') {
            if (empty($_SESSION['Visiter']['visiter_id'])) {
                $arr2['visiter_id'] = str_replace(['+','/', '='] ,'' , base64_encode(pack('Nn', time(), rand(1, 65535))));
                $_SESSION['Visiter']['visiter_id'] = $arr2['visiter_id'];
            } else {
                $arr2['visiter_id'] = $_SESSION['Visiter']['visiter_id'];
            }
        }

        $exclusive_service_name = isset($arr2['service_name']) ? $arr2['service_name'] : '';


        if ($is_mobile) {
            $this->redirect('/mobile/index/home?visiter_id=' . $arr2['visiter_id'] . '&visiter_name=' . $arr2['visiter_name'] . '&avatar=' . $arr2['avatar'] . '&business_id=' . $arr2['business_id'] . '&service_name=' . $exclusive_service_name . '&groupid=' . $arr2['groupid']);
        }

        $business_id = ($arr2['business_id']);
        $visiter_id = $arr2['visiter_id'];

        $channel = bin2hex($visiter_id . '/' . $business_id);
        $visiter_name = ($arr2['visiter_name']);

        $avatar = ($arr2['avatar']);

        if ($visiter_name == '') {
            $visiter_name = '游客' . $visiter_id;
        }

        $groupid = ($arr2['groupid']);

        $app_key = app_key;
        $whost = whost;
        $arr = parse_url($whost);
        if ($arr['scheme'] == 'ws') {

            $port = 'wsPort';
            $value = 'false';
        } else {

            $value = 'true';
            $port = 'wssPort';
        }

        $business = User::table('wolive_business')->where('business_id', $business_id)->find();

        $this->assign("type", $business['video_state']);
        $this->assign("atype", $business['audio_state']);
        $this->assign('app_key', $app_key);
        $this->assign('whost', $arr['host']);
        $this->assign('value', $value);
        $this->assign('wport', wport);;
        $this->assign('port', $port);
        $this->assign('url', $url);
        $this->assign('groupid', $groupid);
        $this->assign('visiter', $visiter_name);
        $this->assign('business_id', $business_id);
        $this->assign('from_url', $from_url);
        $this->assign('channel', $channel);
        $this->assign('visiter_id', $visiter_id);
        $this->assign('exclusive_service_name', $exclusive_service_name);
        $this->assign('avatar', $avatar);

        return $this->fetch();
    }

    /**
     * 404页面
     */

    public function errors()
    {
        return $this->fetch();
    }

    /**
     * 获取排队数量.
     *
     * @return mixed
     */
    public function getwaitnum()
    {
        $post = $this->request->post();
        $num = User::table('wolive_queue')->where('visiter_id', $post['visiter_id'])->where("service_id", 0)->count();
        return $num;
    }


}
